package com.br.gapps.crm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.br.gapps.crm.entity.Customer;
import com.br.gapps.crm.entity.SalesPurchase;
import com.br.gapps.crm.repository.CustomerRepository;

@ManagedBean
@ViewScoped
public class SalesPurchasesBean {

	List<SalesPurchase> salesPurchases = new ArrayList<>();
	SalesPurchase salesPurchase = new SalesPurchase();
	
	public SalesPurchasesBean() {
		// TODO Auto-generated constructor stub
	}

	public List<SalesPurchase> getSalesPurchases() {
		return salesPurchases;
	}

	public void setSalesPurchases(List<SalesPurchase> salesPurchases) {
		this.salesPurchases = salesPurchases;
	}

	public SalesPurchase getSalesPurchase() {
		return salesPurchase;
	}

	public void setSalesPurchase(SalesPurchase salesPurchase) {
		this.salesPurchase = salesPurchase;
	}
	
	public void addSalesPurchase(){
		this.getSalesPurchases().add(this.getSalesPurchase());
		this.setSalesPurchase(new SalesPurchase());
	}
	
	public List<Customer> completeCustomer(String query){
		CustomerRepository customers = new CustomerRepository();
		List<Customer> allCustomers = customers.findAll();
		List<Customer> filteredCustomers = new ArrayList<Customer>();
		for (int i = 0; i < allCustomers.size(); i++) {
			Customer customer = allCustomers.get(i);
            if(customer.getNome().toLowerCase().startsWith(query) || customer.getNome().toUpperCase().startsWith(query)) {
            	filteredCustomers.add(customer);
            }
        }
        return filteredCustomers;
	}

}
